package Controller;

import Controller.LoginAndExitException.NoUserLoginException;
import Controller.LoginAndExitException.WrongPassWordOrIdException;
import Controller.MainState.MainState;
import Controller.MainState.NoUserState;
import Controller.User.*;
import Dao.LoginAndExitDao;
import Dao.ManagerDao;
import Page.MainPage.MainPageViewModel;
import Util.AlertUtil;

import java.sql.SQLException;

/**
 * @since 2024-06-06
 * @author yooo_fan
 */
public class MainController {
    // 当前用户
    private static User currentUser = null;

    // 当前应用状态
    private static MainState currentState = null;

    public static void init(){
        currentUser = null;
        currentState = new NoUserState();
    }

    /**
     * @return 返回当前应用状态
     */
    public static MainState getCurrentState() {
        return currentState;
    }

    /**
     * @return 获取当前用户
     */
    public static User getCurrentUser() {
        return currentUser;
    }

    /**
     * 登录
     * @param userid 用户输入id
     * @param password 用户输入密码
     */
    public static void login(String userid, String password) throws WrongPassWordOrIdException{
        // 如果尚且未登录
        if(currentUser instanceof NoUserState || currentUser == null){
            try{
                // 检查账号密码
                if(LoginAndExitDao.isCorrect(userid, password)){
                    // ======>账号密码通过
                    // ===>检测账号类型
                    if(LoginAndExitDao.isStudent(userid))
                    {
                        // 创建学生用户
                        MainController.currentUser = new StudentUser(userid,password);
                    }else{
                        // 创建教师用户
                        MainController.currentUser = new TeacherUser(userid,password);
                    }

                    // =======>尝试登录
                    //==>登录确认成功
                    if (MainController.currentUser.tryToLogin(userid)) {
                        // 切换状态
                        currentState = currentUser.getMainState();

                        MainPageViewModel.updateHelloText();
                        AlertUtil.showAlert("登录成功","密码正确");
                    } else {
                        AlertUtil.showAlert("登录失败","出现未知错误，请联系管理员");
                    }

                }else {
                    throw new WrongPassWordOrIdException();
                }
            }catch (SQLException e){
                System.err.println("登录发生错误");
            }
        }else{
            // 已经登录拒绝操作
            AlertUtil.showAlert("登录失败","请勿重复登录");
        }
    }

    /**
     * 登出操作
     */
    public static void logout(){
        if(currentUser != null){
            MainController.currentUser.Logout();
            MainController.currentUser = null;
            MainController.currentState = new NoUserState();
        }
        MainPageViewModel.updateHelloText();
    }

    /**
     * 获取当前是否存在登录用户
     * @return 若存在用户登录，返回true，若没有用户登陆返回错误
     * @throws NoUserLoginException 没有用户登陆
     */
    public static Boolean ifHasUser() throws NoUserLoginException {
        if(currentState == null || currentState instanceof NoUserState){
            throw new NoUserLoginException();
        }else {
            return true;
        }
    }

    /**
     * 获取当前账户的类型
     * @return UserType标识类型
     */
    public static UserType getUserType(){
        if(currentUser != null)
        {
            return currentUser.getUSerType();
        }
        return null;
    }

    /**
     * 获取当前用户的ID
     * @return 当前用户ID
     * @throws NoUserLoginException 没有登录用户
     */
    public static String getCurrentUserId() throws NoUserLoginException {
        if(currentState instanceof NoUserState || currentState == null){
            throw new NoUserLoginException();
        }else{
            return MainController.currentUser.getId();
        }
    }

    /**
     * @return 当前用户的名称
     */
    public static String getCurrentUsrName(){
        if(currentUser != null)
        {
            return currentUser.getUsername();
        }
        return null;
    }

    /**
     * 管理员登录验证
     * @param password 用户输入密码
     * @return 状态是否正确
     */
    public static Boolean managerLogin(String password)  throws WrongPassWordOrIdException{
        if(currentUser instanceof NoUserState || currentUser == null){
            try{
                if(password.equals(ManagerDao.getPassword())){
                    MainController.currentUser = new ManagerUser();
                    currentState = currentUser.getMainState();
                    AlertUtil.showAlert("登录成功","你好管理员");
                    MainPageViewModel.updateHelloText();
                    return true;
                }else{
                    throw new WrongPassWordOrIdException();
                }
            }catch (SQLException e){
                MainPageViewModel.updateHelloText();
                return false;
            }
        }
        MainPageViewModel.updateHelloText();
        return false;
    }
}
